/*
 * Copyright (c)  2008 Sebastien Tremblay
 *
 * id3-tag-builder - helpers.FileHelper.java
 *
 * All source and documentation is copyrighted by Sebastien Tremblay
 * (seb at ryders dot net) and made available under the Apache License 2.0.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id: FileHelper.java 30 2010-01-08 04:01:06Z ryders $
 */
package com.empireone.id3tagbuilder.helpers;

import java.io.File;
import java.util.Vector;

/**
 * The <code>FileHelper</code> contains a series of 'toolkit methods' around
 * <see>java.io.File</see>.
 *
 * @author Sebastien Tremblay
 */
public class FileHelper {

    /**
     *
     * @param uri
     * @param ext
     * @return
     */
    public static Vector<File> getFilesByExt(String uri, String ext) {

        Vector<File> files = new Vector<File>();
        File root = new File(uri);
        ext = (ext.startsWith(".") ? "" : ".") + ext.toLowerCase();

        if (root.exists() && root.isDirectory()) {
            for (File f : root.listFiles()) {
                if (f.isDirectory()) {
                    files.addAll(getFilesByExt(f.getPath(), ext));
                } else if (f.getPath().toLowerCase().endsWith(ext) && f.length() > 0) {
                    files.add(f);
                }
            }
        }

        return files;
    }

    /**
     *
     * @param uri
     * @param name
     * @return
     */
    public static File getFileByName(String uri, String name) {
        return getFileByName(uri, name.split(";|,|\\|"));
    }

    /**
     *
     * @param uri
     * @param names
     * @return
     */
    public static File getFileByName(String uri, String[] names) {

        File res = null;
        File root = new File(uri);

        if (root.exists() && root.isDirectory()) {

            search:
            for (File f : root.listFiles()) {
                if (f.isDirectory()) {
                    // @todo: do we ever want to get the 'disc' art? if not, implement FilenameFilter
                    // would require recusivity... 
                    //res = getFileByName(f.getPath(), names);
                    //if (res != null) break search;
                } else {
                    for (int i = 0; i < names.length; i++) {
                        if (f.getName().equalsIgnoreCase(names[i])) {
                            res = f;
                            break search;
                        }
                    }
                }
            }
        }

        return res;
    }
}
